package org.bw.bookstore.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 *@program:wisdom_bookstore
 *@description:日期时间转换工具类
 *@author:孙冬雪
 *@create:2020-10-22 09:50
 */
public class DateTime {

    /**
     * @Description //TODO getDateTime 获取日期时间类型的当前日期/时间
     * @Param [pattern] 
     * @return java.util.Date
     * @Author 孙冬雪
     * @Date 2020/10/6 21:02
     **/
    private static Date getDateTime(String pattern){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date date = new Date();
        String formatDate = simpleDateFormat.format(date);
        return parseDateTime(pattern, formatDate);
    }

    public static Date getCurrentDate(){
        return getDateTime("yyyy-MM-dd");
    }

    public static Date getCurrentTime(){
        return getDateTime("HH:mm:ss");
    }

    /**
     * @Description //TODO getDateTime 获取String类型的当前日期/时间
     * @Param [pattern]
     * @return java.util.Date
     * @Author 孙冬雪
     * @Date 2020/10/6 21:02
     **/
    private static String getStringDateTime(String pattern){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date date = new Date();
        return simpleDateFormat.format(date);
    }

    public static String getStringCurrentDate(){
        return getStringDateTime("yyyy-MM-dd");
    }

    public static String getStringCurrentTime(){
        return getStringDateTime("HH:mm:ss");
    }

    /**
     * @Description //TODO parseDateTime 根据传入字符串，获取日期时间类型的当前日期/时间
     * @Param [pattern, datetime]
     * @return java.util.Date
     * @Author 孙冬雪
     * @Date 2020/10/6 22:36
     **/
    private static Date parseDateTime(String pattern, String datetime){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date parse = null;
        try {
            parse =simpleDateFormat.parse(datetime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    public static Date parseDate(String date){
        return parseDateTime("yyyy-MM-dd",date);
    }

    public static Date parseTime(String time){
        return parseDateTime("HH:mm:ss",time);
    }

    /**
     * @Description //TODO formatTimeByString 传入Long类型返回String类型日期
     * @Param [time]
     * @return java.lang.String
     * @Author 孙冬雪
     * @Date 2020/10/6 22:39
     **/
    public static String formatTimeByLong(Long time){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(time);
    }

    /**
     * @Description //TODO formatFutureDate 返回day天后的String类型的日期
     * @Param [day] 
     * @return java.lang.String
     * @Author 孙冬雪
     * @Date 2020/10/7 20:47
     **/
    public static String formatFutureDate(Integer day){
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH,day);
        return DateTime.formatTimeByLong(c.getTime().getTime());
    }
}
